import Vue from 'vue';
import VueRouter from 'vue-router';
import iView from 'iview';
import Util from './libs/util';

Vue.use(VueRouter);

const routers = [
	{
		path: '*',
		component: (resolve) => require(['./views/error404.vue'], resolve)
	},
	{
	    path: '/',
	    meta: {
	        title: '我的操作日志'
	    },
	    component: (resolve) => require(['./views/index.vue'], resolve)
	},
	{
	    path: '/index',
	    meta: {
	        title: '我的操作日志'
	    },
	    component: (resolve) => require(['./views/index.vue'], resolve)
	},
	{
	    path: '/test',
	    meta: {
	        title: 'test'
	    },
	    component: (resolve) => require(['./views/test.vue'], resolve)
	},
	{
	    path: '/authorization',
	    meta: {
	        title: 'authorization'
	    },
	    component: (resolve) => require(['./views/authorization/code.vue'], resolve)
	},
	{
	    path: '/base',
	    meta: {
	        title: 'base',
	        requiresAuth: true
	    },
	    component: (resolve) => require(['./views/base/base.vue'], resolve),
	    children: [
	    	{
	    		path: 'welcome',
                name: 'welcome',
                component: (resolve) => require(['./views/sys/welcome.vue'], resolve),
                meta: {
                    title:"welcome"
                }
	    	},
	    	{
	    		path: 'user',
                name: 'user',
                component: (resolve) => require(['./views/sys/user.vue'], resolve),
                meta: {
                    title:"user"
                }
	    	},
	    	{
	    		path: 'menu',
                name: 'menu',
                component: (resolve) => require(['./views/sys/menu.vue'], resolve),
                meta: {
                    title:"menu"
                }
	    	},
	    	{
	    		path: 'role',
                name: 'role',
                component: (resolve) => require(['./views/sys/role.vue'], resolve),
                meta: {
                    title:"role"
                }
	    	},
	    ]
	},
	{
		path: '/dp',
		meta: {
			title: 'dp',
			requiresAuth: true
		},
		component: (resolve) => require(['./views/base/base.vue'], resolve),
		children: [
			{
				path: 'list',
				name: 'list',
				component: (resolve) => require(['./views/dp/list.vue'], resolve),
				meta: {
					title:"指数列表"
				}
			},
			{
				path: 'index_daily',
				name: 'index_daily',
				component: (resolve) => require(['./views/dp/index_daily.vue'], resolve),
				meta: {
					title:"指数日K"
				}
			}
		]
	},
	{
		path: '/example',
		meta: {
			title: 'example',
			requiresAuth: false
		},
		component: (resolve) => require(['./views/base/base.vue'], resolve),
		children: [
			{
				path: 'myecharts',
				name: 'myecharts',
				component: (resolve) => require(['./views/example/myecharts.vue'], resolve),
				meta: {
					title:"echarts样例"
				}
			},
			{
				path: 'my_table',
				name: 'my_table',
				component: (resolve) => require(['./views/example/my_table.vue'], resolve),
				meta: {
					title:"表格编辑样例"
				}
			}
		]
	},
	{
		path: '/my',
		meta: {
			title: '交易管理',
			requiresAuth: true
		},
		component: (resolve) => require(['./views/base/base.vue'], resolve),
		children: [
			{
				path: 'history',
				name: 'history',
				component: (resolve) => require(['./views/my/history.vue'], resolve),
				meta: {
					title:"历史操作"
				}
			},
			{
				path: 'stock_history',
				name: 'stock_history',
				component: (resolve) => require(['./views/my/stock_history.vue'], resolve),
				meta: {
					title:"个股盈亏"
				}
			},
			{
				path: 'cash_history',
				name: 'cash_history',
				component: (resolve) => require(['./views/my/cash_history.vue'], resolve),
				meta: {
					title:"转入转出"
				}
			},
			{
				path: 'analyse',
				name: 'analyse',
				component: (resolve) => require(['./views/my/analyse.vue'], resolve),
				meta: {
					title:"操作分析"
				}
			}
		]
	},
	{
		path: '/one',
		meta: {
			title: 'one',
			requiresAuth: true
		},
		component: (resolve) => require(['./views/base/base.vue'], resolve),
		children: [
			{
				path: 'list',
				name: 'list',
				component: (resolve) => require(['./views/one/list.vue'], resolve),
				meta: {
					title:"个股列表"
				}
			},
			{
				path: 'daily_data',
				name: 'daily_data',
				component: (resolve) => require(['./views/one/daily_data.vue'], resolve),
				meta: {
					title:"个股日K"
				}
			},
			{
				path: 'analyse',
				name: 'analyse',
				component: (resolve) => require(['./views/one/analyse.vue'], resolve),
				meta: {
					title:"个股分析"
				}
			}
		]
	},
	{
		path: '/mange',
		meta: {
			title: 'mange',
			requiresAuth: true
		},
		component: (resolve) => require(['./views/base/base.vue'], resolve),
		children: [
			{
				path: 'masterStockInfo',
				name: 'masterStockInfo',
				component: (resolve) => require(['./views/mange/masterStockInfo.vue'], resolve),
				meta: {
					title:"个股列表"
				}
			},
			{
				path: 'oneStockInfo',
				name: 'oneStockInfo',
				component: (resolve) => require(['./views/mange/oneStockInfo.vue'], resolve),
				meta: {
					title:"个股分析"
				}
			}
		]
	}
];

// 路由配置
const RouterConfig = {
    mode: 'history',
    routes: routers
};
const router = new VueRouter(RouterConfig);

router.beforeEach((to, from, next) => {

    let token = window.localStorage.getItem('currentUser_token');
    if (to.matched.some(record => record.meta.requiresAuth) && (!token || token === null)) {
        next({
          path: '/',
          query: { redirect: to.fullPath }
        })
    }/* else {
        next()
        console.log("next");
    }*/
    iView.LoadingBar.start();
    Util.title(to.meta.title);
    next();
});

router.afterEach(() => {
    iView.LoadingBar.finish();
    window.scrollTo(0, 0);
});

export default router;
